﻿using System;
using System.Collections.Generic;
using System.Text;
using He.Common;
using System.Data.SQLite;
using System.Data;

namespace He.DAL
{
    public partial class wx_menu
    {
        public readonly string TblName = SysKeys.TABLE_PREFIX + "wx_menu";

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            DbHelperSQLite.UpdateField(TblName, id, strValue);
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        public bool DeleteAll(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + TblName);
            strSql.Append(" where id=@id or pid=@pid");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4),
                    new SQLiteParameter("@pid", DbType.Int32,4)
			};
            parameters[0].Value = id;
            parameters[1].Value = id;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 取得所有菜单列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataTable GetList(int pid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + TblName);
            strSql.Append(" order by sort_id asc,id desc");
            DataSet ds = DbHelperSQLite.Query(strSql.ToString());
            DataTable oldData = ds.Tables[0] as DataTable;
            if (oldData == null)
            {
                return null;
            }

            //复制结构
            DataTable newData = oldData.Clone();
            //调用迭代组合成DAGATABLE
            GetChild(oldData, newData, pid);
            return newData;
        }

        /// <summary>
        /// 从内存中取得所有下级菜单列表（自身迭代）
        /// </summary>
        private void GetChild(DataTable oldData, DataTable newData, int pid)
        {
            DataRow[] dr = oldData.Select("pid=" + pid);
            for (int i = 0; i < dr.Length; i++)
            {
                //添加一行数据
                DataRow row = newData.NewRow();
                row["id"] = int.Parse(dr[i]["id"].ToString());
                row["pid"] = int.Parse(dr[i]["pid"].ToString());
                row["type"] = dr[i]["type"].ToString();
                row["name"] = dr[i]["name"].ToString();
                row["key"] = dr[i]["key"].ToString();
                row["url"] = dr[i]["url"].ToString();
                row["sort_id"] = int.Parse(dr[i]["sort_id"].ToString());
                row["is_lock"] = int.Parse(dr[i]["is_lock"].ToString());

                newData.Rows.Add(row);
                //调用自身迭代
                this.GetChild(oldData, newData, int.Parse(dr[i]["id"].ToString()));
            }
        }

        /// <summary>
        /// 是否存在显示的子菜单
        /// </summary>
        public bool ExistsChild(int pid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + TblName);
            strSql.Append(" where pid=@pid");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@pid", DbType.Int32,4)
			};
            parameters[0].Value = pid;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int top, string strWhere)
        {
            return DbHelperSQLite.GetList(TblName, top, "*", strWhere, "sort_id asc,id desc");
        }
    }
}
